//
//  WQCBillViewController.m
//  renxinghuan
//
//  Created by 王晓民 on 2018/3/16.
//  Copyright © 2018年 吴清翠. All rights reserved.
//

#import "WQCBillViewController.h"
#import "configuration.h"
#import "BillModel.h"
@interface WQCBillViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView  *billTabelView;
/** data数组*/
@property(nonatomic,strong)NSMutableArray * datasourArray;

@property(nonatomic,assign)BOOL Display;
/** details数组*/
@property(nonatomic,strong)NSMutableArray *detailsArray;

@end

@implementation WQCBillViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.datasourArray = [[NSMutableArray alloc]init];
    [self setupNav];
    [self loadBill];
    self.billTabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, -20, kScreenW, kScreenH) style:UITableViewStyleGrouped];
    self.billTabelView.dataSource = self;
    self.billTabelView.delegate = self;
    [self.view addSubview:self.billTabelView];
    [self setTableView];
}

-(void)setTableView
{
//    self.billTabelView
     __weak __typeof(self) weakSelf = self;
    self.billTabelView.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
       // _pageIndex=0;
      //  weakSelf.refreshState=NO;
        [weakSelf loadBill];
    }];
    
}


- (void)setupNav {
    
    [self.navigationController.navigationBar setBarTintColor:wycMain];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationItem.title = @"账单";
}
#pragma mark - TableView DataSource Method

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datasourArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * strID = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:strID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strID];
    }
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    BillModel * taranLationModel = self.datasourArray[indexPath.row];

    UILabel *label= [MyUIClass makeUILabelWithFrame:CGRectMake(80, 10, 120, 20) andBackColor:[UIColor clearColor] andText:[NSString stringWithFormat:@"%@",taranLationModel.amount] andTextColor:[UIColor blackColor ] andFont:[UIFont systemFontOfSize:18] andAlignment:NSTextAlignmentLeft];
    [cell.contentView addSubview:label];
    UILabel *timelabel= [MyUIClass makeUILabelWithFrame:CGRectMake(10, 40, 180, 20) andBackColor:[UIColor clearColor] andText:taranLationModel.createTime andTextColor:[UIColor lightGrayColor] andFont:[UIFont systemFontOfSize:12] andAlignment:NSTextAlignmentLeft];
   [cell.contentView addSubview:timelabel];
    
    UILabel *banklabel= [MyUIClass makeUILabelWithFrame:CGRectMake(ScreenWidth-130, 30, 120, 20) andBackColor:[UIColor clearColor] andText:[NSString stringWithFormat:@"[%@(%@)]",taranLationModel.bankName,taranLationModel.bankLastNum] andTextColor:[UIColor blackColor ] andFont:[UIFont systemFontOfSize:12] andAlignment:NSTextAlignmentRight];
    [cell.contentView addSubview:banklabel];
    
    UIImageView  *typeImg = [MyUIClass makeUIImageViewWithFrame:CGRectMake(10, 10, 35, 20) andImage:@""];
    NSString  *type = taranLationModel.type;
    if ([type isEqualToString:@"1"]) {
        typeImg.image = [UIImage imageNamed:@"consumption"];
        label.text = [NSString stringWithFormat:@"-%.1f",[taranLationModel.amount doubleValue]];
    }
    else if([type isEqualToString:@"2"])
    {
        typeImg.image = [UIImage imageNamed:@"repayment"];
        label.text = [NSString stringWithFormat:@"+%.1f",[taranLationModel.amount doubleValue]];
    }
    else if ([type isEqualToString:@"3"])
    {
        typeImg.image = [UIImage imageNamed:@"amount"];

        banklabel.text =@"[分润]";
        label.text = [NSString stringWithFormat:@"+%.1f",[taranLationModel.amount doubleValue]];

    }
    else if ([type isEqualToString:@"4"])
    {
        typeImg.image = [UIImage imageNamed:@"money"];

        label.text = [NSString stringWithFormat:@"-%.1f",[taranLationModel.amount doubleValue]];
    }
    
    [cell.contentView addSubview:typeImg];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
-(void)loadBill
{
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"type"] = @"0";
    params[@"currentPage"] = @"1";

    [HttpTool POST:getCardPlan params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"zhangd%@",responseObject);
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            [self.datasourArray removeAllObjects];
            NSDictionary * dict = responseObject[@"result"];
            NSArray * dataArray = dict[@"data"];
            
            for (NSDictionary * dic in dataArray)
            {
                BillModel * taranLationModel = [BillModel mj_objectWithKeyValues:dic];
                [self.datasourArray addObject:taranLationModel];
                
            }
            [_billTabelView reloadData];
            [self.billTabelView.mj_header endRefreshing];
        }else{
            
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
